﻿using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;

namespace RuoVea.ExWeb.Jwt
{
    /// <summary>
    /// 注入 Jwt
    /// </summary>
    public static class JwtOption
    {
        /// <summary>
        /// 注入jwt
        /// </summary>
        /// <param name="services"></param>
        /// <param name="Configuration"></param>
        public static void JwtOptiong(this IServiceCollection services, IConfiguration Configuration)
        {
            JWTEncryption.setting(Configuration);

            #region Jwt token Authentication

            services.AddAuthentication(options =>
            {
                options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
                options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;

            }).AddJwtBearer(configureOptions =>
            {
                JWTSettingsOptions jwtSetting = JWTEncryption.GetJWTSettings();
                configureOptions.ClaimsIssuer = jwtSetting.ValidIssuer;
                configureOptions.TokenValidationParameters = JWTEncryption.CreateTokenValidationParameters(jwtSetting);
                configureOptions.SaveToken = true;
            });

            //    .AddJwtBearer(configureOptions => {
            //    var Issuer = Appsettings.app("JwtSetting", "Issuer").ToString();
            //    var Audience = Appsettings.app("JwtSetting", "Audience").ToString();
            //    var SecretKey = Appsettings.app("JwtSetting", "SecretKey").ToString();
            //    configureOptions.ClaimsIssuer = Issuer;
            //    configureOptions.TokenValidationParameters = new TokenValidationParameters
            //    {
            //        ValidateIssuer = true,
            //        ValidIssuer = Issuer,
            //        ValidateAudience = true,
            //        ValidAudience = Audience,
            //        ValidateIssuerSigningKey = true,
            //        IssuerSigningKey = new SymmetricSecurityKey(System.Text.Encoding.UTF8.GetBytes(SecretKey)),
            //        RequireExpirationTime = true,
            //        ValidateLifetime = true,
            //        ClockSkew = TimeSpan.Zero
            //    };
            //    configureOptions.SaveToken = true;
            //});
            #endregion
        }
    }
}
